#include "highlighter.h"

highlighter::highlighter(QTextDocument *parent): QSyntaxHighlighter(parent){

	highlightingRule rule;
	QStringList geometricPatterns;
	QStringList coordsPatterns;

	geometricFormat.setForeground(Qt::red);
	rule.pattern = QRegExp("G0?[0123]");
	rule.format = geometricFormat;
	highlightingRules.append(rule);
	
	coordsFormat.setForeground(Qt::blue);
	rule.pattern = QRegExp("[XYZ](?=-?\\d+.?\\d*)");
	rule.format = coordsFormat;
	highlightingRules.append(rule);
	
	coordsArcFormat.setForeground(Qt::green);
	rule.pattern = QRegExp("[IJK](?=-?\\d+.?\\d*)");
	rule.format = coordsArcFormat;
	highlightingRules.append(rule);
	
	radiusArcFormat.setForeground(Qt::green);
	rule.pattern = QRegExp("[R](?=-?\\d+.?\\d*)");
	rule.format = coordsArcFormat;
	highlightingRules.append(rule);
	
	paramFormat.setForeground(Qt::gray);
	rule.pattern = QRegExp("F\\d+.?\\d*");
	rule.format = paramFormat;
	highlightingRules.append(rule);
	rule.pattern = QRegExp("S\\d+");
	rule.format = paramFormat;
	highlightingRules.append(rule);
	
	commentFormat.setForeground(Qt::magenta);
	rule.pattern = QRegExp("\\( *#!?OBJECT +[a-zA-Z0-9_-]+ *\\)");
	rule.format = commentFormat;
	highlightingRules.append(rule);

    rule.pattern = QRegExp(";START +[a-zA-Z0-9_-]+");
    rule.format = commentFormat;
    highlightingRules.append(rule);

    rule.pattern = QRegExp(";END +[a-zA-Z0-9_-]+");
    rule.format = commentFormat;
    highlightingRules.append(rule);

}

void highlighter::highlightBlock(const QString &text){
	
	foreach(const highlightingRule &rule, highlightingRules){
		QRegExp expression(rule.pattern);
		int index = expression.indexIn(text);
		while (index >= 0){
			int length = expression.matchedLength();
			setFormat(index, length, rule.format);
			index = expression.indexIn(text, index + length);
		}
	}
}
